package com.itheima;

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapDemo {
    public  static void main(String[] args)
    {
        test1();
        System.out.println("------------------------");
        test2();
    }
    public static void test1()
    {
        TreeMap<Student,String> treeMap = new TreeMap<Student,String>();

        treeMap.put(new Student("小王",18),"北京");
        treeMap.put(new Student("小李",19),"上海");
        treeMap.put(new Student("小张",20),"广州");
        treeMap.put(new Student("小赵",21),"深圳");

        treeMap.forEach((key,value)->System.out.println(key+"---"+value));
        System.out.println("------------------------------------------------");
        for (Student key : treeMap.keySet()) {
            String value = treeMap.get(key);
            System.out.println(key+"---"+value);

        }
    }
    public static void test2()
    {
        TreeMap<Student,String> treeMap = new TreeMap<Student,String>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge()-o1.getAge();
            }
        });

        treeMap.put(new Student("小王",18),"北京");
        treeMap.put(new Student("小李",19),"上海");
        treeMap.put(new Student("小张",20),"广州");
        treeMap.put(new Student("小赵",21),"深圳");

        treeMap.forEach((key,value)->System.out.println(key+"---"+value));
    }
}
